_l(f)н@ӵrڲr˱ҊԴaSĿһͬl(f)҂_l(f)rĭh(hun)ch(hun)ܲһ£电(sh)(j)첻һӣڴaб@Щ@ЩϢrҪÑĴa¾g@Nʽdz韩.netṩһNݵıĿϢkǾļļļYһ.configasp.netļһĬJweb.configÿweb.configļǻXMLıļҿԱ浽WebóеκĿڰl(f)Webórweb.configļgMdllļ͑˰l(f)׃HHҪӛ±_web.configļPOþͿʹãdz
ƪҪv֪R£
ļIJ҃(yu)ȼ
ļ(ji)cf
ļIJ
ļIJ҃(yu)ȼ
.netṩһᘌǰCļ@ļmachine.configλ%windir%\Microsoft.NET\Framework\v2.0.50727\CONFIG\ļ£%windir%ϵy(tng)օ^(q)µϵy(tng)Ŀ䛣ģʽݔ%windir%Ȼ܇ܲ鿴ǰCϵy(tng)ĿWindows2003WindowsXP%windir%ϵy(tng)օ^(q)µwindowsĿ䛣Windows2000%windir%ϵy(tng)օ^(q)µWinNTĿڹPߙC@ϵy(tng)ĿC:\WINDOWS@ļ涨xᘌǰCWinFormasp.netómachine.configļăݣ
@ļA߀һweb.configļ@ļasp.netWվijá@web.configļăݣ
asp.netWվIISӵĕrdļеϢȻ@ЩϢ@ӾͲÿȥxȡϢ\^asp.netóO(jin)ҕļ׃rһ@ЩϢ͕xȡ@ЩϢ
҂Ҫxȡij(ji)c߹(ji)cMϢrǰ·ʽģ
(1)ڮǰĿ´web.configļ鿴ǷҪҵĽYcQڷؽYֹͣ
(2)ǰĿ²web.configļweb.configļвԓYctϼĿֱWվĸĿ䛡
(3)WվĿ²web.configļweb.configļвԓ(ji)ct%windir%\Microsoft.NET\Framework\v2.0.50727\CONFIG\web.configļв
(4)%windir%\Microsoft.NET\Framework\v2.0.50727\CONFIG\web.configļвYct%windir%\Microsoft.NET\Framework\v2.0.50727\CONFIG\machine.configļв
(5)Ȼ]ҵtnull
҂ijWվijļAضҪļA(chung)һweb.configļwϼļAеweb.configļеͬü@ЩϢČֻһԺ㱻{asp.netó\^Уweb.configļl(f)ľ͕đó@r惦ڷȴеÑԒϢ͕Gʧ惦ڃȴеSessionһЩܛ皢ܛÿɌweb.configLr͕web.configLrgҲasp.netó؆
ļ(ji)cf
web.configļһXMLļĸYc<configuration><configuration>(ji)cµijҊӹ(ji)cУ<configSections><appSettings><connectionStrings><system.web> <appSettings>(ji)cҪһЩWվđϢ<connectionStrings>(ji)cҪþWվĔ(sh)(j)BַϢ
<system.web>(ji)cҪǾWվ\ЕrһЩijҊ(ji)c£
<appSettings>(ji)c
<appSettings>(ji)cҪÁ惦asp.netóһЩϢςļı·һӣ
<appSettings>
<!--SςĈDƬʽ-->
<add key="ImageType" value=".jpg;.bmp;.gif;.png;.jpeg"/>
<!--Sςļ-->
<add key="FileType" value=".jpg;.bmp;.gif;.png;.jpeg;.pdf;.zip;.rar;.xls;.doc"/>
</appSettings>
<appSettings>(ji)cеֵkeyML¾һxȡkeyֵ顰FileType(ji)cֵӣ
string fileType=ConfigurationManager.AppSettings["FileType "];
<connectionStrings>(ji)c
<connectionStrings>(ji)cҪÔ(sh)(j)Bӵģ҂<connectionStrings>(ji)c₀(ji)c攵(sh)(j)Bַڴaͨ^aķʽӑB(ti)@ȡ(ji)cֵ팍(sh)(j)Bӌ@һĕr(sh)(j)BϢl(f)׃҂HҪĴ̎ü锵(sh)(j)BϢ׃ҪĄӳa²
¾һ<connectionStrings>(ji)cõӣ
<connectionStrings>
<!--SQL Server(sh)(j)-->
<add name="AspNetStudyConnectionString1" connectionString="Data Source=(local);Initial Catalog=AspNetStudy;User ID=sa;Password=sa"/>
</connectionStrings>
ڴa҂@ô(sh)(j)Bӌ
//xȡweb.config(ji)c
string connectionString = ConfigurationManager.ConnectionStrings["AspNetStudyConnectionString1"].ConnectionString;
//SqlConnection
SqlConnection connection = new SqlConnection(connectionString);
@ĺ̎һ_l(f)rõĔ(sh)(j)ͲrĔ(sh)(j)첻һHHҪӛ±֮ı߾connectionStringԵֵ
<compilation>(ji)c
<compilation>(ji)c ASP.NET ʹõоgOáĬJdebugԞ顰trueS{ԇ@NrӰ푾Wվڳgɽʹ֮O顰false
<authentication>(ji)cOasp.netCģʽķNCģʽֵքe£
Mode f
Windows ʹWindowsCmÑ߾WÑ
Forms ʹñCWվ_l(f)ˆTMC
Passport ʹܛṩCMC
None MκC
<authentication>(ji)c
<authentication>(ji)cÑWվĿ䛻߆Ϊ퓵L<authentication>(ji)cһʹ
<customErrors>(ji)c
<customErrors>(ji)cڶxһЩԶxe`ϢϢ˹(ji)cModedefaultRedirectɂdefaultRedirectһxԣʾól(f)e`rضĬJURL]ָԓԄt@ʾһe`Modeһxֵxքe£
Mode f
On ʾڱغhÑԶxe`Ϣ
Off Զxe`ϢغhÑԔe`Ϣ
RemoteOnly ʾÑԔe`ϢhÑԶxe`Ϣ
@бҪfһ±ÑhÑĸ҂Lasp.netó̕rʹõęCͰl(f)asp.netóʹõęCͬһ_Crɞ鱾Ñ֮tQ֮hÑ_l(f){ԇAΞ˱ڲe`ModeԽhOÞOffڲAΑModeOÞOnRemoteOnlyԱ@ЩԔe`Ϣ¶˳a(ji)Ķڿ͵
҂һCustomErrorsDemo.aspxPage_Load¼һa£
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class CustomErrorsDemo : System.Web.UI.Page
{
void Page_Load() void Page_Load(object sender, EventArgs e)
{
throw new Exception("⒁Į");
}
}
҂<customErrors>£
<customErrors mode="RemoteOnly">
<error statusCode="403" redirect="NoAccess.htm" />
<error statusCode="404" redirect="FileNotFound.htm" />
</customErrors>
@r\CustomErrorsDemo.aspxЧ£
hLrЧ
҂customErrorsModeOÞ顰On\кhLЧ
customErrorsModeOÞ顰Off\кhLЧ
<error>ӹ(ji)c
<customErrors>(ji)c߀<error>ӹ(ji)c@(ji)cҪǸ(j)HTTPe`B(ti)aض҂Զxe`עҪʹ<error>ӹ(ji)cµЧ회<customErrors>(ji)c(ji)cModeOÞ顰Onһӣ
<customErrors mode="On" defaultRedirect="GenericErrorPage.htm">
<error statusCode="403" redirect="403.htm" />
<error statusCode="404" redirect="404.htm" />
</customErrors>
ÑL治ھ͕D404.htm棬Ñ]ЙLՈtD403.htm403.htm404.htm涼҂Լӵ҂нoѺõe`ʾ
<httpHandlers>(ji)c
<httpHandlers>(ji)cڸ(j)ÑՈURLHTTP^~ÑՈo̎üeκΌӴô˹(ji)cҲfᘌijضĿָļM̎
cmachine.configļͬһĿµweb.configļе<httpHandlers>(ji)cã
<httpHandlers>
<add path="*.rules" verb="*" type="System.Web.HttpForbiddenHandler" validate="true"/>
<add path="*.xoml" verb="*" type="System.ServiceModel.Activation.HttpHandler, System.ServiceModel, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" validate="false"/>
<add path="trace.axd" verb="*" type="System.Web.Handlers.TraceHandler" validate="true"/>
<add path="WebResource.axd" verb="GET" type="System.Web.Handlers.AssemblyResourceLoader" validate="true"/>
<add path="*.axd" verb="*" type="System.Web.HttpNotFoundHandler" validate="true"/>
<add path="*.aspx" verb="*" type="System.Web.UI.PageHandlerFactory" validate="true"/>
<add path="*.ashx" verb="*" type="System.Web.UI.SimpleHandlerFactory" validate="true"/>
<add path="*.asmx" verb="*" type="System.Web.Services.Protocols.WebServiceHandlerFactory, System.Web.Services, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" validate="false"/>
<add path="*.rem" verb="*" type="System.Runtime.Remoting.Channels.Http.HttpRemotingHandlerFactory, System.Runtime.Remoting, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" validate="false"/>
<add path="*.soap" verb="*" type="System.Runtime.Remoting.Channels.Http.HttpRemotingHandlerFactory, System.Runtime.Remoting, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" validate="false"/>
<add path="*.asax" verb="*" type="System.Web.HttpForbiddenHandler" validate="true"/>
<add path="*.ascx" verb="*" type="System.Web.HttpForbiddenHandler" validate="true"/>
<add path="*.master" verb="*" type="System.Web.HttpForbiddenHandler" validate="true"/>
<add path="*.skin" verb="*" type="System.Web.HttpForbiddenHandler" validate="true"/>
<add path="*.browser" verb="*" type="System.Web.HttpForbiddenHandler" validate="true"/>
<add path="*.sitemap" verb="*" type="System.Web.HttpForbiddenHandler" validate="true"/>
<add path="*.dll.config" verb="GET,HEAD" type="System.Web.StaticFileHandler" validate="true"/>
<add path="*.exe.config" verb="GET,HEAD" type="System.Web.StaticFileHandler" validate="true"/>
<add path="*.config" verb="*" type="System.Web.HttpForbiddenHandler" validate="true"/>
<add path="*.cs" verb="*" type="System.Web.HttpForbiddenHandler" validate="true"/>
<add path="*.csproj" verb="*" type="System.Web.HttpForbiddenHandler" validate="true"/>
<add path="*.vb" verb="*" type="System.Web.HttpForbiddenHandler" validate="true"/>
<add path="*.vbproj" verb="*" type="System.Web.HttpForbiddenHandler" validate="true"/>
<add path="*.webinfo" verb="*" type="System.Web.HttpForbiddenHandler" validate="true"/>
<add path="*.licx" verb="*" type="System.Web.HttpForbiddenHandler" validate="true"/>
<add path="*.resx" verb="*" type="System.Web.HttpForbiddenHandler" validate="true"/>
<add path="*.resources" verb="*" type="System.Web.HttpForbiddenHandler" validate="true"/>
<add path="*.mdb" verb="*" type="System.Web.HttpForbiddenHandler" validate="true"/>
<add path="*.vjsproj" verb="*" type="System.Web.HttpForbiddenHandler" validate="true"/>
<add path="*.java" verb="*" type="System.Web.HttpForbiddenHandler" validate="true"/>
<add path="*.jsl" verb="*" type="System.Web.HttpForbiddenHandler" validate="true"/>
<add path="*.ldb" verb="*" type="System.Web.HttpForbiddenHandler" validate="true"/>
<add path="*.ad" verb="*" type="System.Web.HttpForbiddenHandler" validate="true"/>
<add path="*.dd" verb="*" type="System.Web.HttpForbiddenHandler" validate="true"/>
<add path="*.ldd" verb="*" type="System.Web.HttpForbiddenHandler" validate="true"/>
<add path="*.sd" verb="*" type="System.Web.HttpForbiddenHandler" validate="true"/>
<add path="*.cd" verb="*" type="System.Web.HttpForbiddenHandler" validate="true"/>
<add path="*.adprototype" verb="*" type="System.Web.HttpForbiddenHandler" validate="true"/>
<add path="*.lddprototype" verb="*" type="System.Web.HttpForbiddenHandler" validate="true"/>
<add path="*.sdm" verb="*" type="System.Web.HttpForbiddenHandler" validate="true"/>
<add path="*.sdmDocument" verb="*" type="System.Web.HttpForbiddenHandler" validate="true"/>
<add path="*.mdf" verb="*" type="System.Web.HttpForbiddenHandler" validate="true"/>
<add path="*.ldf" verb="*" type="System.Web.HttpForbiddenHandler" validate="true"/>
<add path="*.exclude" verb="*" type="System.Web.HttpForbiddenHandler" validate="true"/>
<add path="*.refresh" verb="*" type="System.Web.HttpForbiddenHandler" validate="true"/>
<add path="*.svc" verb="*" type="System.ServiceModel.Activation.HttpHandler, System.ServiceModel, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" validate="false"/>
<add path="*" verb="GET,HEAD,POST" type="System.Web.DefaultHttpHandler" validate="true"/>
<add path="*" verb="*" type="System.Web.HttpMethodNotAllowedHandler" validate="true"/>
</httpHandlers>
пԿᘌ*.mdf*.ldfļGetPostՈoSystem.Web.HttpForbiddenHandler̎̎ĽYÑܲ鿴dPļ҂ijļAµļij͵ļSÑd</httpHandlers>(ji)cӹ(ji)c
҂һӁf<httpHandlers>(ji)c÷҂asp.netóнһIPDataĿIPDataĿЄ(chung)һIPData.txtļȻWeb.configã
<httpHandlers>
<add path="IPData/*.txt" verb="*" type="System.Web.HttpForbiddenHandler"/>
</httpHandlers>
ĴaǽֹLIPDataĿµκtxtļ
Ȼ½һһ朽ӣ朽ӵԓĿIPData.txtļa£
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="HttpHandlersDemo.aspx.cs" Inherits="HttpHandlersDemo" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>httpHandlers(ji)c</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<a href="IPData/IPData.txt" title="_IPData/IPData.txt">_IPData/IPData.txt</a>
</div>
</form>
</body>
</html>
\@Ч£
ǰweb.configļ<customErrors>(ji)c£
<customErrors mode="On" defaultRedirect="GenericErrorPage.htm">
<error statusCode="403" redirect="403.htm" />
<error statusCode="404" redirect="404.htm" />
</customErrors>
403.htm404.htmc朽֮F(xin)Ч
҂ψDпԿ<customErrors>(ji)cModeԞ顰Onr鱻ֹLIPDataļAµtxtļԕDԶxě]Йʾ403.htm
<httpRuntime>(ji)c
<httpRuntime>(ji)cڌ ASP.NET HTTP \ЎOԓ(ji)ӋCվcóĿ䛼e
ÿÑςļ40M40*1024Krrg60l(f)Ո100
<httpRuntime maxRequestLength="40960" executionTimeout="60" appRequestQueueLimit="100"/>
<pages>(ji)c
<pages>(ji)cڱʾضOҪքe£
f
buffer Ƿ HTTP 푑_
enableViewStateMac Ƿԓ퓵ҕDB(ti)\ӋCCz (MAC)ԷÑ۸ĬJfalseOÞtrueܵĽ
validateRequest ǷCÑݔпվc_SQLעʽ©ĬJtrueF(xin)ƥr͕l(f) HttpRequestValidationException ڰھıһCÑݔˌOfalse
һù(ji)cӣ
<pages buffer="true" enableViewStateMac="true" validateRequest="false"/>
<sessionState>(ji)c
<sessionState>(ji)cîǰasp.netóĕԒB(ti)á¾һҊã
<sessionState cookieless="false" mode="InProc" timeout="30" />
Ĺ(ji)cOasp.netóІCookieָԒB(ti)ģʽMбԒB(ti)ͬr߀ָ˕Ԓr30
<sessionState>(ji)cModeԿNֵ֮һ
ֵ f
Custom ʹԶx(sh)(j)惦ԒB(ti)(sh)(j)
InProc ĬJֵasp.netoḾ惦ԒB(ti)(sh)(j)
Off ÕԒB(ti)
SQLServer ʹMSQL Server(sh)(j)챣ԒB(ti)(sh)(j)
StateServer ʹM ASP.NET B(ti)մ惦B(ti)Ϣ
һĬJrʹInProcģʽ惦ԒB(ti)(sh)(j)@Nģʽĺ̎Ǵȡٶȿ죬ȱcDZ^ռÃȴԲ@Nģʽ´惦͵ÑԒ(sh)(j)
<globalization>(ji)c
ÑóȫO˹(ji)cЎׂ^ҪČքe£
f
fileEncoding xԡO.aspx.asmx .asax ļĴ惦a
requestEncoding xԡOÿ͑ՈľaĬJUTF-8.
responseEncoding xԡO÷푑ľaĬJUTF-8.
¾asp.netóеĬJã
<globalization fileEncoding="utf-8" requestEncoding="utf-8" responseEncoding="utf-8"/>
ļx
mȻweb.configļһXMLļڙԭڲвͨXMLļǘM.netṩһڌweb.configM
ᘌweb.configͨĴa
using System;
using System.Configuration;
using System.Web;
using System.Web.Configuration;
/// <summary>
/// ConfigurationOperator ժҪf
/// </summary>
public class ConfigurationOperator:IDisposable
{
private Configuration config;
ConfigurationOperator() ConfigurationOperator():this(HttpContext.Current.Request.ApplicationPath)
{
}
ConfigurationOperator() ConfigurationOperator(string path)
{
config = WebConfigurationManager.OpenWebConfiguration(path);
}
/// <summary>
/// OÑóù(ji)cѽڴ˹(ji)ctԓ(ji)cֵtӴ˹(ji)c
/// </summary>
/// <param name="key">(ji)cQ</param>
/// <param name="value">(ji)cֵ</param>
void SetAppSetting() void SetAppSetting(string key, string value)
{
AppSettingsSection appSetting = (AppSettingsSection)config.GetSection("appSettings");
if (appSetting.Settings[key] == null)//ڴ˹(ji)ct
{
appSetting.Settings.Add(key, value);
}
else//ڴ˹(ji)ct
{
appSetting.Settings[key].Value = value;
}
}
/// <summary>
/// OÔ(sh)(j)Bַ(ji)cڴ˹(ji)ctӴ˹(ji)cֵڄt
/// </summary>
/// <param name="key">(ji)cQ</param>
/// <param name="value">(ji)cֵ</param>
void SetConnectionString() void SetConnectionString(string key, string connectionString)
{
ConnectionStringsSection connectionSetting = (ConnectionStringsSection)config.GetSection("connectionStrings");
if (connectionSetting.ConnectionStrings[key] == null)//ڴ˹(ji)ct
{
ConnectionStringSettings connectionStringSettings = new ConnectionStringSettings(key, connectionString);
connectionSetting.ConnectionStrings.Add(connectionStringSettings);
}
else//ڴ˹(ji)ct
{
connectionSetting.ConnectionStrings[key].ConnectionString = connectionString;
}
}
/// <summary>
///
/// </summary>
void Save() void Save()
{
config.Save();
config = null;
}
void Dispose() void Dispose()
{
if (config != null)
{
config.Save();
}
}
}
ĴaŵApp_CodeļA£҂ĿоͿֱʹ
҂ͨ^һʾʹ@ͨweb.configMOá½һaspxǰ_a
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ConfigModifyDemo.aspx.cs" Inherits="ConfigModifyDemo" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>ڲweb.config</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table border="0" cellpadding="0" cellspacing="0">
<tr><td></td><td>Q</td><td>ֵ</td></tr>
<tr><td>
</td><td>
<asp:TextBox ID="txtKey" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="txtKey"
ErrorMessage="*" Display="Dynamic"></asp:RequiredFieldValidator></td><td>
<asp:TextBox ID="txtAppSetting" runat="server"></asp:TextBox></td></tr>
<tr><td>
(sh)(j)B</td><td>
<asp:TextBox ID="txtConnectionName" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="*" ControlToValidate="txtConnectionName" Display="Dynamic"></asp:RequiredFieldValidator></td><td style="height: 24px">
<asp:TextBox ID="txtConnectionString" runat="server"></asp:TextBox></td></tr>
<tr><td>
<asp:Button ID="btnModify" runat="server" OnClick="btnModify_Click" Text="" /></td><td></td><td></td></tr>
</table>
</div>
</form>
</body>
</html>
_aЕrҪӌļxdllã£
Ǻ_a
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.Configuration;//ע@g
public partial class ConfigModifyDemo : System.Web.UI.Page
{
void Page_Load() void Page_Load(object sender, EventArgs e)
{
}
void btnModify_Click() void btnModify_Click(object sender, EventArgs e)
{
string appSetting = txtAppSetting.Text;//appSettingӹ(ji)cֵ
string connectionString = txtConnectionString.Text;//Bַ
string key = txtKey.Text;//appSettingӹ(ji)cKey
string connectionName = txtConnectionName.Text;//BName
ConfigurationOperator op = new ConfigurationOperator();
op.SetAppSetting(key, appSetting);
op.SetConnectionString(connectionName, connectionString);
op.Save();
}
}
\н棺
҂ıϢ
O˕rweb.configļP(ji)că£
<appSettings>
</appSettings>
<connectionStrings>
<add name="Conn" connectionString="Data Source=(local);Initial Catalog=AspNetStudy;Persist Security Info=True;User ID=sa;Password=sa" />
</connectionStrings>
҂cġo֮ļ£
<appSettings>
<add key="country" value="china" />
</appSettings>
<connectionStrings>
<add name="Conn" connectionString="Data Source=(local);Initial Catalog=Study;User ID=sa;Password=sa"
providerName="System.Data.SqlClient" />
</connectionStrings>
Ĉ(zh)нYԿ҂ij_ĺweb.configеĹ(ji)cĹҪעijЩ汾ܛ֮Microsoft Visual SourceSafe汾ܛܕweb.configOÞֻx͕F(xin)Oõr҂Ҫքӌweb.configֻxȥOweb.configļڌHĿĕrͲ@}
Yweb.configasp.netóһҪļͨ^web.configļԷ҂M_l(f)Ͳasp.netó߀܌MһЩ`ĿڱƪԔv˸(ji)cáڲasp.netóԭܰXMLʽMweb.configļڱƪ߀ṩһᘌ<appSettings>(ji)c<connectionStrings>(ji)cOõͨxѿԸ(j)HĿҪ@ͨMƺa